package com.stox.charting.ui.swing.event.listener;

import com.stox.charting.ui.swing.event.ChartModelChangedEvent;
import com.stox.charting.ui.swing.model.BarType;
import com.stox.charting.ui.swing.model.ChartModel;
import com.stox.charting.ui.swing.model.ChartModel.ChartModelListener;
import com.stox.charting.ui.swing.model.price.PriceChartModel;
import com.stox.charting.ui.swing.model.price.PriceChartView;
import com.stox.charting.ui.swing.widget.BarTypeComboBox;
import com.stox.core.model.BarValueType;
import com.stox.core.model.IBar;
import com.stox.core.util.ListUtil;
import com.stox.ioc.Container;

public class DataChangedEventListener implements ChartModelListener {

	
	@Override
	@SuppressWarnings("unchecked")
	public void chartModelChanged(ChartModelChangedEvent event) {
		if(event.getProperty().equalsIgnoreCase("data")){
			
			if(PriceChartModel.class.isAssignableFrom(event.getSource().getClass())){
				PriceChartModel<? extends IBar> priceChartModel = (PriceChartModel<? extends IBar>)event.getSource();
				PriceChartView view = priceChartModel.getChart().getChartView();
				view.getVolumeChart().getModel().setData(ListUtil.values(priceChartModel.getData(), BarValueType.VOLUME));
				
				BarTypeComboBox barTypeComboBox = Container.get(BarTypeComboBox.class);
				priceChartModel.setEventsEnabled(false);
				priceChartModel.setBarType((BarType)barTypeComboBox.getItemAt(barTypeComboBox.getSelectedIndex()));
				priceChartModel.setEventsEnabled(true);
				priceChartModel.fireChartModelChangedEvent("barType");
				
				/*JRangeSlider rangeSlider = priceChartModel.getChart().getChartView().getChartingInternalFrameView().getRangeSlider();
				rangeSlider.setMinimum(0);
				rangeSlider.setMaximum(priceChartModel.getData().size());
				rangeSlider.setLowValue(rangeSlider.getMinimum());
				rangeSlider.setHighValue(rangeSlider.getMaximum());*/
			}else{
				ChartModel<?> chartModel = (ChartModel<?>)event.getSource();
				chartModel.constructUiUnits();
			}
		}
	}
	
}
